/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * assignmentMakerGUI.java
 *
 * Created on Jul 17, 2010, 10:29:28 PM
 */

package gui;

import data.hostInfoStorage;
import data.runConfig;
import java.io.File;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.JFileChooser;

/**
 * Gui to add assignments
 *
 * @todo add number parsing checking
 * @todo fix bug: needs extra \n at end of textBox
 * @author Todd Bodnar
 */
public class assignmentMakerGUI extends javax.swing.JFrame {

    /** Creates new form assignmentMakerGUI */
    public assignmentMakerGUI(hostInfoStorage hostData, hostGui host) {
        data = hostData;
        myHost = host;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        openButton = new javax.swing.JButton();
        openText = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        paramListTextBox = new javax.swing.JTextArea();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        repTextBox = new javax.swing.JTextField();
        jSeparator3 = new javax.swing.JSeparator();
        jLabel4 = new javax.swing.JLabel();
        saveButton = new javax.swing.JButton();
        saveText = new javax.swing.JTextField();
        jSeparator4 = new javax.swing.JSeparator();
        jLabel5 = new javax.swing.JLabel();
        genCheckBox = new javax.swing.JCheckBox();
        jSeparator5 = new javax.swing.JSeparator();
        jLabel6 = new javax.swing.JLabel();
        addButton = new javax.swing.JButton();
        tickCt = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jSeparator6 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("1.");

        openButton.setText(" Open .nLogo file");
        openButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openButtonActionPerformed(evt);
            }
        });

        openText.setEditable(false);

        jLabel2.setText("2.    Define Parameters   (example  variable,val1,val2,val3)");

        paramListTextBox.setColumns(20);
        paramListTextBox.setRows(5);
        paramListTextBox.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                paramListTextBoxKeyTyped(evt);
            }
        });
        jScrollPane1.setViewportView(paramListTextBox);

        jLabel3.setText("3.    Repetitions");

        repTextBox.setText("1");

        jLabel4.setText("4.");

        saveButton.setText("Choose save location");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        saveText.setEditable(false);

        jLabel5.setText("5.  ");

        genCheckBox.setText("Generate individual files");

        jLabel6.setText("7.");

        addButton.setText("Add 1 experiment to queue");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        tickCt.setText("100");

        jLabel7.setText("6.   Simulation length");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(openButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(openText, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addContainerGap(107, Short.MAX_VALUE))
            .addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addGap(18, 18, 18)
                .addComponent(repTextBox, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(230, Short.MAX_VALUE))
            .addComponent(jSeparator3, javax.swing.GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(saveButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(saveText, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(jSeparator4, javax.swing.GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(genCheckBox)
                .addContainerGap(236, Short.MAX_VALUE))
            .addComponent(jSeparator5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 381, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(tickCt, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(178, Short.MAX_VALUE))
            .addComponent(jSeparator6, javax.swing.GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(addButton)
                .addContainerGap(214, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(openButton)
                    .addComponent(openText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(repTextBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(saveButton)
                    .addComponent(saveText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(genCheckBox))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator5, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(tickCt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator6, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(addButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void paramListTextBoxKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_paramListTextBoxKeyTyped
        int ct = generateCountOfAssignments();
        if(ct==1)
            addButton.setText("Add 1 experiment to queue");
        else
            addButton.setText("Add "+ct+" experiments to queue");
    }//GEN-LAST:event_paramListTextBoxKeyTyped

    private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openButtonActionPerformed
        JFileChooser jfc = new JFileChooser();
        jfc.showOpenDialog(null);

        if(jfc.getSelectedFile()==null)
            return;

        openText.setText(jfc.getSelectedFile().toString());
        openFile = jfc.getSelectedFile();
    }//GEN-LAST:event_openButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        JFileChooser jfc = new JFileChooser();
        jfc.showSaveDialog(null);

        if(jfc.getSelectedFile()==null)
            return;

        saveText.setText(jfc.getSelectedFile().toString());
        saveLocation = jfc.getSelectedFile();
    }//GEN-LAST:event_saveButtonActionPerformed

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
        Vector<Vector<String>> lines = new Vector<Vector<String>>();
        Vector<runConfig> Assignments = new Vector<runConfig>();

        StringTokenizer paramTokenizer = new StringTokenizer(paramListTextBox.getText(),"\n");
        while(paramTokenizer.hasMoreTokens())
        {
            String line = paramTokenizer.nextToken();

            StringTokenizer elements = new StringTokenizer(line,",");

            Vector<String> runs = new Vector<String>();

            while(elements.hasMoreElements())
                runs.add(elements.nextToken());

            if(runs.size()>1)
                lines.add(runs);
        }

        int totalRuns = generateCountOfAssignments();
        int pointers[] = new int[lines.size()];
        for(int ct=0;ct<pointers.length;ct++)
            pointers[ct]=1;
        
        while(Assignments.size()!=totalRuns)
        {
            for(int c=0;c<Integer.parseInt(repTextBox.getText());c++)
            {
                Vector<String> commands = new Vector<String>();
                for(int ct=0;ct<pointers.length;ct++)
                {
                    commands.add(lines.get(ct).get(0)+" "+lines.get(ct).get(pointers[ct]));
                }

                //----------------todo, add custom file name generation code

                File outputName;
                if(genCheckBox.isSelected())
                {
                    StringTokenizer st = new StringTokenizer(saveLocation.toString(),".");
                    String name = st.nextToken();
                    for(int ct=0;ct<commands.size();ct++)
                        name += " "+commands.get(ct);

                    name+=" run "+c;
                    name+="."+st.nextToken();
                    outputName = new File(name);
                }
                else
                    outputName = saveLocation;
                Assignments.add(new runConfig(openFile, outputName.toString(), Long.parseLong(tickCt.getText()), commands));
            }
            pointers[pointers.length-1]++;
            for(int c=pointers.length-1;c>0;c--)
                if(pointers[c]>=lines.get(c).size())
                {
                    pointers[c]=1;
                    pointers[c-1]++;
                }
        }

        for(int ct=0;ct<Assignments.size();ct++)
            data.addTodo(Assignments.elementAt(ct));

        myHost.updateView();
    }//GEN-LAST:event_addButtonActionPerformed

    private int generateCountOfAssignments()
    {
        int combinations = 1;

        combinations = combinations * Integer.parseInt(repTextBox.getText());

        StringTokenizer lines = new StringTokenizer(paramListTextBox.getText(),"\n");
        while(lines.hasMoreTokens())
        {
            String line = lines.nextToken();

            StringTokenizer elements = new StringTokenizer(line,",");

            elements.nextToken();

            combinations*= elements.countTokens();
        }

        return combinations;
    }

    private File openFile, saveLocation;
    private hostInfoStorage data;
    private hostGui myHost;

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JCheckBox genCheckBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JSeparator jSeparator5;
    private javax.swing.JSeparator jSeparator6;
    private javax.swing.JButton openButton;
    private javax.swing.JTextField openText;
    private javax.swing.JTextArea paramListTextBox;
    private javax.swing.JTextField repTextBox;
    private javax.swing.JButton saveButton;
    private javax.swing.JTextField saveText;
    private javax.swing.JTextField tickCt;
    // End of variables declaration//GEN-END:variables

}
